// pages/Personal/collect/collect.js
var ikz = require('../../Common/tools.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    atyp:'tou',//显示分类
    color_tou:'#fff',//头条字色
    bg_tou: 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)',//头条背景色
    color_bian: '#000',//便民字色
    bg_bian: '#fff',//便民背景色
    page: 1,//页数
    limit: 6,//每页条数
    head_info: '',//头条信息
    conven_info: '',//便民信息
    prodis: 'none',//底部提示是否显示
  },
  
  

  //数字转换
  changeNum(num) {
    if (num > 0 && num < 10000) {
      num = num / 100;
      num = Math.ceil(num) / 10;
      var newnum = num + 'k';
    } else if (num >= 10000) {
      num = num / 1000;
      num = Math.ceil(num) / 10;
      var newnum = num + 'w';
    } else {
      var newnum = 0;
    }
    return newnum;
  },
  /**
   * 点击分类
   */
  toType: function (e) {
    var atyp = e.currentTarget.dataset.atyp;
    if (atyp == 'tou') {
      this.setData({
        color_tou: '#fff',//头条字色
        bg_tou: 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)',//头条背景色
        color_bian: '#000',//便民字色
        bg_bian: '#fff',//便民背景色
        atyp: atyp,
        page: 1,
        prodis: 'none',
      })
    } else {
      this.setData({
        color_tou: '#000',//头条字色
        bg_tou: '#fff',//头条背景色
        color_bian: '#fff',//便民字色
        bg_bian: 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)',//便民背景色
        atyp: atyp,
        page: 1,
        prodis: 'none',
      })
    }
  },
  // 头条详情页
  toContent: function (e) {
    var that = this;
    var hid = e.currentTarget.dataset.hid;
    wx.navigateTo({
      url: '../../Home/pageDetail/pageDetail?' + 'hid=' + hid,
    })
  },
  //跳转到详情页
  toContents: function (e) {
    var cid = e.currentTarget.dataset.cid;
    var data = {
      cid: cid,
    }
    ikz.nav('../../Conveinfo/content/content', 'n', data);
  },
  // 置顶
  toTop: function () {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // ikz.over(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var page = 1;
    var limit = that.data.limit;
    var path = 'Percenter/myCollect';
    var data = {
      page: page,
      limit: limit,
    }
    ikz.request(path, data, true, (res) => {

      console.log(res.data)
      if (res.data.status == 'success') {
        console.log(res.data)
        that.setData({
          head_info: res.data.head_info,
          conven_info: res.data.conven_info,
          page: page,
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '加载中',
    });
    var that = this;
    var page = that.data.page + 1;
    var limit = that.data.limit;
    var atyp = that.data.atyp;
    var old_head_info = that.data.head_info;
    var old_conven_info = that.data.conven_info;
    var path = 'Percenter/myCollect';
    var data = {
      page: page,
      limit: limit,
    }

    ikz.request(path, data, true, (res) => {

      if (res.data.status == 'success') {
        if (atyp == 'tou') {
          var new_info = res.data.head_info;
          console.log(res);
          console.log(6767);
          if (new_info.length) {
            var new_head_info = old_head_info.concat(new_info);
            var prodis = 'none';
          } else {
            var new_head_info = old_head_info;
            var prodis = 'block';
          }
          var new_conven_info = old_conven_info;
        } else {
          var new_info = res.data.conven_info;
          var new_head_info = old_head_info;
          if (new_info.length) {
            var new_conven_info = old_conven_info.concat(new_info);
            var prodis = 'none';
          } else {
            var new_conven_info = old_conven_info;
            var prodis = 'block';
          }
        }

        that.setData({
          head_info: new_head_info,//所有头条信息
          conven_info: new_conven_info,//所有头条信息
          page: page,
          prodis: prodis,
        })
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})